package com.mybatispuls.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mybatispuls.springboot.Utils.R;
import com.mybatispuls.springboot.entity.EduBook;
import com.mybatispuls.springboot.entity.EduOrder;
import com.mybatispuls.springboot.entity.UcenterMember;
import com.mybatispuls.springboot.service.impl.EduBookServiceImpl;
import com.mybatispuls.springboot.service.impl.EduOrderServiceImpl;
import com.mybatispuls.springboot.service.impl.UcenterMemberServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

@Controller
public class logincontroller {
    @Autowired
    private EduBookServiceImpl eduBookService;

    @Autowired
    private UcenterMemberServiceImpl ucenterMemberService;

    @Autowired
    private EduOrderServiceImpl eduOrderService;

    @RequestMapping("log")
    public String log(){
        return "login";
    }

    @RequestMapping("up")
    public String up(){
        return "uplode";
    }

    @RequestMapping("in")
    public String in(){
        return "index";
    }

    @PostMapping("login")
    public String login(UcenterMember ucenterMember, HttpServletRequest request){
        QueryWrapper<UcenterMember> Wrapper = new QueryWrapper<>();
        String mobile = ucenterMember.getMobile();
        Wrapper.eq("mobile",mobile);
        List<UcenterMember> list = ucenterMemberService.list(Wrapper);
        if (list!=null){
            String nickname = list.get(0).getNickname();
            String sign = list.get(0).getSign();
            System.out.println(sign+"--------------------------------------------");
            System.out.println(nickname);
            request.getSession().setAttribute("nickname",nickname);
            request.getSession().setAttribute("sign",sign);

            return "redirect:/bookList";
        }else {
            return "login";
        }





    }







    @GetMapping("bookList")
    public String bookList(Model model){
        List<EduBook> list = eduBookService.list(null);
        model.addAttribute("list",list);
        return "index";
    }

    //根据id查询
    @GetMapping("book/{id}")
    public String seleteById(@PathVariable String id,Model model){
        EduBook book = eduBookService.getById(id);
        model.addAttribute("book",book);
        return "list";
    }

    @GetMapping("save/{id}")
    public String save(@PathVariable String id, EduOrder eduOrder,HttpServletRequest request){
        EduBook book = eduBookService.getById(id);
        String bookname = book.getBookname();
        String author = book.getAuthor();
        String press = book.getPress();
        BigDecimal price = book.getPrice();
        eduOrder.setBookname(bookname);
        eduOrder.setAuthor(author);
        eduOrder.setPress(press);
        eduOrder.setPrice(price);
        String nickname = request.getSession().getAttribute("nickname").toString();
        String sign = request.getSession().getAttribute("sign").toString();
        System.out.println(nickname);
        eduOrder.setUsername(nickname);
        eduOrder.setAddress(sign);
        eduOrderService.save(eduOrder);
        return "redirect:/bookList";
    }

    @GetMapping("index")
    public String ind(){
        return "index";
    }





}
